#!/bin/bash

gcov_repo_path='/srv/www/htdocs/gcov'

function make_gcov_data_publish()
{
    release_dir=`get_release_dir`
    publish_dir="$release_dir/gcov"

    cd $CPATH
    rm -rf gcov_data 
    mkdir gcov_data
    for line in $(echo "EulerOS:V3R1:BuildToolsBep EulerOS:V3R1:COMMOM EulerOS:V3R1:CompileTools EulerOS:V3R1:GENERAL EulerOS:V3R1:GENERAL:Custom EulerOS:V3R1:GENERAL:Kernel EulerOS:V3R1:KIWI EulerOS:V3R1:PANGEA EulerOS:V3R1:PANGEA:Custom EulerOS:V3R1:PANGEA:Kernel EulerOS:V3R1:PANGEA:Storage EulerOS:V3R1:STANDARD EulerOS:V3R1:STANDARD:Docker EulerOS:V3R1:STANDARD:LibStorage EulerOS:V3R1:UVP")
    do
        sh "$TOOLS"/sshscp.sh -p "huawei" -s "10.175.100.158:$gcov_repo_path/$line" -d "$CPATH/gcov_data" -r
    done

    tar -czvf gcov_data.tar.gz gcov_data
    create_checksum gcov_data.tar.gz
    SSH_CMD="mkdir -p $publish_dir"
    sshcmd "$SSH_CMD"
    sshscp "gcov_data.tar.gz gcov_data.tar.gz${SHA256SUM}" "$publish_dir"
    rm -rf gcov_data*
}

function update_sys_custom_tool()
{
    pushd /opt/obs_workerspace/sys-custom-tool
    flag_pri=5
    middle_dir="./sys-custom-tool-1.0.0/local_script/compile_env_x86/yum.repos.d"
    ls $middle_dir | while read line
    do
        cp -a ${middle_dir}/${line} ${middle_dir}/gcov_${line}
        name=`echo ${middle_dir}/${line} | awk -F '/' '{print $NF}'  | awk -F '.' '{print $1}'`
        sed -i "s#$name#gcov_${name}#g" ${middle_dir}/gcov_${line}
        sed -i "s#priority=.*#priority=${flag_pri}#" ${middle_dir}/gcov_${line}
        let "flag_pri = $flag_pri +1"
    done
    modify_for_gcov './'
    source_module=`ls | grep -v *.spec`
    tar -czf ${source_module}.tar.gz ${source_module} --remove-files
    popd

    rm -rf EulerOS:V3R1:GENERAL:Custom
expect -c "
    set timeout 300
    spawn osc co EulerOS:V3R1:GENERAL:Custom sys-custom-tool
    expect {
        \"Enter choice*:\" {send \"2\r\"; exp_continue}
         eof { catch wait result; exit [lindex \$result 3] }
    }
    expect {
        eof { catch wait result; exit [lindex \$result 3] }
    }
"
    if [ $? -ne 0 ]; then
        log_error "osc co EulerOS:V3R1:GENERAL:Custom sys-custom-tool failed"
    fi
    pushd EulerOS:V3R1:GENERAL:Custom/sys-custom-tool
    osc rm *
    cp /opt/obs_workerspace/sys-custom-tool/* .
    osc add ${source_module}.tar.gz *.spec
    newfiles=`osc status | grep ? | awk '{print $2}'`
    if [ -n "$newfiles" ]; then
        osc add $newfiles
    fi
    osc ci -m"CI_build"
    if [ $? -ne 0 ]; then
        log_error "osc ci in EulerOS:V3R1:GENERAL:Custom/sys-custom-tool failed"
    fi
}

function update_sys_custom_tool_indocker()
{
    docker_init
    docker_pull ${EulerOS_dockerhub}/x00173555/euleros_build:1.0
    work_path=$(cd ${CPATH}/../../../../../;pwd)
    relative_path=`echo $CPATH | sed "s#$work_path/##g"`
    docker_run --rm -t --privileged=true -v /dev:/dev -v /sys:/sys -v ${work_path}:/opt/obs_workerspace ${EulerOS_dockerhub}/x00173555/euleros_build:1.0 \
    bash --login -c "pushd /opt/obs_workerspace/$relative_path;bash make_version.sh update_sys_custom_tool_in_docker"
    result=$?
    if [ $result -ne 0 ]; then
        log_error "Failed on update_sys-custom-tool_in_docker"
    else
        log_info "Suessessful on update_sys-custom-tool_in_docker"
    fi
    return $result
}
